package com.water.camera.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.plata.base.aclr.utils.SPHelper;
import com.water.camera.bean.MediaData;
import com.water.camera.databinding.ActivityBatchEditBinding;
import com.water.camera.fragment.BaseDialogFragment;
import com.water.camera.fragment.MarkDlg;
import com.water.camera.it.WaterMarkViewChangeListener;
import com.water.camera.mark.BaseMark;
import com.water.camera.util.ToastUtil;
import com.water.camera.util.Utils;

import java.util.ArrayList;
import java.util.List;

public class BatchEditAct extends BaseSaveAct implements WaterMarkViewChangeListener {

    ActivityBatchEditBinding binding;
    BaseMark mMark = null;
    int index;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityBatchEditBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        binding.ivClose.setOnClickListener(v -> finish());

        index = getIntent().getIntExtra("index", 0);
        ArrayList<MediaData> list = getIntent().getParcelableArrayListExtra("img");
        MediaData data = list.get(index);
        Glide.with(this).load(data.getPath()).into(binding.ivCover);
        String name = data.getMark();
        float x, y;
        if (TextUtils.isEmpty(name)) {
            name = getIntent().getStringExtra("mark");
            x = getIntent().getFloatExtra("x", 0f);
            y = getIntent().getFloatExtra("y", 0f);
        } else {
            x = data.getGlLeftBottomX();
            y = data.getGlLeftBottomY();
        }
        if (!TextUtils.isEmpty(name)) {
            try {
                BaseMark view = (BaseMark) Class.forName(name).getDeclaredConstructor(Context.class, AttributeSet.class)
                        .newInstance(this, null);
                String key = view.getClass().getSimpleName() + "_scale";
                float scale = SPHelper.getInstance(this).getFloat(key, 1.0f);
                view.setScale(scale);
                onWaterMarkViewChange(view);
                mMark.setTranslationX(x);
                mMark.setTranslationY(y);
            } catch (Throwable e) {
                e.printStackTrace();
            }
        }
        binding.btnWaterMask.setOnClickListener(v -> chooseWaterMark());
        binding.btnSave.setOnClickListener(v -> {
            if (mMark == null) {
                ToastUtil.showShortToast(this, "尚未添加水印");
                return;
            }
            List<MediaData> tmp = new ArrayList<>();
            tmp.add(list.get(index));
            save(mMark, binding.waterView, tmp);
        });
    }

    private void chooseWaterMark() {
        MarkDlg dlg = new MarkDlg();
        dlg.setOnDismissListener(new BaseDialogFragment.OnDismissListener() {
            @Override
            public void onDismiss(boolean isOk, Object value) {

            }
        });
        dlg.show(getSupportFragmentManager(), "");
    }

    @Override
    protected boolean paddingTop() {
        return false;
    }

    @Override
    public void onWaterMarkViewChange(BaseMark mark) {
        binding.waterView.removeAllViews();
        if (mark != null) {
            mark.setTouchable(true);
            FrameLayout.LayoutParams params;
            if (mark.isFullScreen()) {
                params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            } else if (mark.isFullWidth()) {
                params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                params.gravity = Gravity.BOTTOM;
                params.bottomMargin = Utils.dp2Px(this, 20);
            } else {
                params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                params.gravity = Gravity.BOTTOM;
                params.bottomMargin = Utils.dp2Px(this, 20);
                params.leftMargin = params.bottomMargin;
            }
            binding.waterView.addView(mark, params);
        }
        mMark = mark;
    }

    @Override
    protected void finishSave() {
        Intent i = new Intent();
        i.putExtra("index", index);
        i.putExtra("path", output.get(0));
        i.putExtra("x", mMark.getTranslationX());
        i.putExtra("y", mMark.getTranslationY());
        i.putExtra("mark", mMark.getClass().getName());
        setResult(RESULT_OK, i);
        finish();
    }

    @Override
    public boolean needScale() {
        return true;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 102) {//editMarkActivity
            if (resultCode == Activity.RESULT_OK) {
                mMark.update();
                onWaterMarkViewChange(mMark);
            }
        }
    }
}
